package Begin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description:简单的单线程服务器实现四次挥手演示
 * @Author:AMark
 * @Date: 10/28/2023
 */
public class SingleThreadServer2 {

    public static final int PORT = 8899;

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket();

        serverSocket.bind(new InetSocketAddress(PORT));

        System.out.println("server is start at " + PORT);

        while (true) {
            try (
                    Socket socket = serverSocket.accept();
                    InputStream inputStream = socket.getInputStream();
            ) {
                System.out.println("connect success " + socket.getPort());

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                System.out.println("receive from client: " + reader.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

